/*	
   ______   ______    
  /\__  __\/\ \___\ 
  \/_/\ \_/\ \ \__/    __   __     __       
     \ \ \  \ \ \__\  /\ \ /\ \  /'__`\  
     _\_\ \  \ \ \_/_ \ \ \\_\ \/\  __/       
    /\____/   \ \_____\\ \_____/\ \____\   
    \_/__/     \/_____/ \/____/  \/____/                   
 
 	This file is part of the JEVE application API.

    JEve is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    JEve is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    Copyright 2008 JEve Project (JEveProject@gmail.com)
 */
package be.fomp.jeve.application.data.standard.alliancelist;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;

import be.fomp.jeve.application.data.AbstractResultImpl;
import be.fomp.jeve.application.data.standard.AllianceList;
/**
 * <pre>
 *  ______   ______
 * /\__  __\/\ \___\
 * \/_/\ \_/\ \ \__/    __   __     __
 *    \ \ \  \ \ \__\  /\ \ /\ \  /'__`\
 *    _\_\ \  \ \ \_/_ \ \ \\_\ \/\  __/
 *   /\____/   \ \_____\\ \_____/\ \____\
 *   \_/__/     \/_____/ \/____/  \/____/
 * </pre>
 * This file is part of the JEVE application API.<br />
 * <br />
 * <p/>
 * Implementation of the AllianceListTemplate Interface.
 * 
 * @author Sven Meys
 */
public final class AllianceListImpl extends AbstractResultImpl implements AllianceList {
	static final long serialVersionUID = 1L;
	
	/** A list of all alliances */
	private List<Alliance> allianceList = new ArrayList<Alliance>();

	/**
	 * Main and only constructor.<br />
	 * Takes JDOM Element objects and reads them into a list of alliance Objects.<br />
	 * @param xmlDocument The JDOM elemen containing the alliance list data.
	 */
	@SuppressWarnings("unchecked")
	public AllianceListImpl(Document xmlDocument) {
		super(xmlDocument);

		Element rootElement = xmlDocument.getRootElement();
		Element resultSet = rootElement.getChild("result").getChild("rowset");

		List<Element> alliances = resultSet.getChildren();

		for (Element allianceElement : alliances) {
			Alliance alliance = new AllianceImpl(allianceElement);

			this.allianceList.add(alliance);
		}
	}


	public List<Alliance> getAllianceList() {
		return this.allianceList;
	}

	/**
	 * Needed for serialization
	 * @param os The stream to serialize to
	 */
	public void writeObject(ObjectOutputStream os) {
		try {
			os.defaultWriteObject();
			os.writeObject(this.allianceList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Needed for serialization
	 * @param is The stram to deserialize from
	 */
	@SuppressWarnings("unchecked")
	public void readObject(ObjectInputStream is) {
		try {
			is.defaultReadObject();
			this.allianceList = ((List<Alliance>) is.readObject());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
